package com.himore.api.web.controller.modules;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.himore.api.common.core.domain.entity.SysUser;
import com.himore.api.common.utils.CommonUtils;
import com.himore.api.common.utils.DateUtils;
import com.himore.api.common.utils.SecurityUtils;
import com.himore.api.system.service.ISysUserService;
import com.himore.api.web.model.TSysTask;
import com.himore.api.web.service.ITSysTaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.himore.api.common.annotation.Log;
import com.himore.api.common.core.controller.BaseController;
import com.himore.api.common.core.domain.AjaxResult;
import com.himore.api.common.enums.BusinessType;
import com.himore.api.common.utils.poi.ExcelUtil;
import com.himore.api.common.core.page.TableDataInfo;

/**
 * 任务信息Controller
 * 
 * @author ruoyi
 * @date 2021-01-14
 */
@RestController
@RequestMapping("/system/task")
public class TSysTaskController extends BaseController
{
    @Autowired
    private ITSysTaskService tSysTaskService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询任务信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TSysTask tSysTask){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysUser currentUser =userService.selectUserById(sysUser.getUserId());
        startPage();
        Map<String, Date> params = CommonUtils.getWeekDate();
        Date signBeginDate = params.get("mondayDate");
        Date signEndDate = params.get("sundayDate");
        signEndDate = DateUtils.addTimeTodate(signEndDate,1);
        tSysTask.setUserId(sysUser.getUserId());
        tSysTask.setSigndateBegin(DateUtils.getYYYYMMDDDate(signBeginDate));
        tSysTask.setSigndateEnd(DateUtils.getYYYYMMDDDate(signEndDate));
        tSysTask.setUserIdentifNum(currentUser.getUserIdentifNum());
        List<TSysTask> list = tSysTaskService.selectTSysTaskList(tSysTask);
        return getDataTable(list);
    }

    /**
     * 导出任务信息列表
     */
    @Log(title = "任务信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSysTask tSysTask){
        List<TSysTask> list = tSysTaskService.selectTSysTaskList(tSysTask);
        ExcelUtil<TSysTask> util = new ExcelUtil<TSysTask>(TSysTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取任务信息详细信息
     */
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId){
        return AjaxResult.success(tSysTaskService.selectTSysTaskById(taskId));
    }

    /**
     * 新增任务信息
     */
    @Log(title = "任务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSysTask tSysTask){
        return toAjax(tSysTaskService.insertTSysTask(tSysTask));
    }

    /**
     * 修改任务信息
     */
    @Log(title = "任务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSysTask tSysTask){
        return toAjax(tSysTaskService.updateTSysTask(tSysTask));
    }

    /**
     * 删除任务信息
     */
    @Log(title = "任务信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds){
        return toAjax(tSysTaskService.deleteTSysTaskByIds(taskIds));
    }
}
